app.controller("z-punchTime-c", [ '$scope','$rootScope','$z-http','$sce','z-punchTime-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,MenuService,$filter) {
		$scope.viewModel = {};

		$scope.editModel = {};
		$scope.editModel.punchTime={};
		$scope.editModel.title="";
		$scope.editModel.punchType=0;
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'punchTime'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getPunchTimeList({},
			function(data){
				$scope.viewModel.dataList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		

        
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
			
			$('#pStartFromTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
						$scope.editForm.startFromTime.$valid=true;
						$scope.editForm.startFromTime.$dirty=true;
						$scope.editForm.startFromTime.$invalid=false;
						$scope.editModel.punchTime.startFromTime=$('#pStartFromTime').val();
				}	
			});
			$('#pStartToTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
						$scope.editForm.startToTime.$valid=true;
						$scope.editForm.startToTime.$dirty=true;
						$scope.editForm.startToTime.$invalid=false;
						$scope.editModel.punchTime.startToTime=$('#pStartToTime').val();
				}	
			});
			$('#pEndFromTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
						$scope.editForm.endFromTime.$valid=true;
						$scope.editForm.endFromTime.$dirty=true;
						$scope.editForm.endFromTime.$invalid=false;
						$scope.editModel.punchTime.endFromTime=$('#pEndFromTime').val();
				}	
			});
			$('#pEndToTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
						$scope.editForm.endToTime.$valid=true;
						$scope.editForm.endToTime.$dirty=true;
						$scope.editForm.endToTime.$invalid=false;
						$scope.editModel.punchTime.endToTime=$('#pEndToTime').val();
				}	
			});
			
			$('#pRestFromTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
						$scope.editForm.restFromTime.$valid=true;
						$scope.editForm.restFromTime.$dirty=true;
						$scope.editForm.restFromTime.$invalid=false;
						$scope.editModel.punchTime.restFromTime=$('#pRestFromTime').val();
				}	
			});
			$('#pRestToTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
						$scope.editForm.restToTime.$valid=true;
						$scope.editForm.restToTime.$dirty=true;
						$scope.editForm.restToTime.$invalid=false;	
						$scope.editModel.punchTime.restToTime=$('#pRestToTime').val();
				}	
			});
			
		}
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};
		
		//新增
		$scope.viewModel.add = function(retroctive){
			$scope.editModel.title="新增考勤时间设置";
			$scope.editModel.type = true;
			$scope.editModel.punchTime={};
			$scope.editModel.punchTime.isDefault=0;
			$scope.editModel.punchTime.restFromTime="12:00";
			$scope.editModel.punchTime.restToTime="12:00";
			$scope.viewModel.dirty();
			$scope.viewModel.load();
		};
		
		//修正
		$scope.viewModel.update=function(punchTime){
			$scope.editModel.title="修改考勤时间设置";
			$scope.editModel.type = false;
			$scope.editModel.punchTime=angular.copy(punchTime);
			$scope.viewModel.dirty();
			$scope.viewModel.load();
		};
	
		$scope.viewModel.dirty=function(){
			//验证不能为空
			$scope.editForm.punchTimeName.$dirty=false;
			$scope.editForm.startFromTime.$dirty=false;
			$scope.editForm.startToTime.$dirty=false;
			$scope.editForm.endFromTime.$dirty=false;
			$scope.editForm.endToTime.$dirty=false;
			$scope.editForm.startInterval.$dirty=false;
			$scope.editForm.endInterval.$dirty=false;
			$scope.editForm.restFromTime.$dirty=false;
			$scope.editForm.restToTime.$dirty=false;
		};
	      //格式化验证
        $scope.viewModel.escapeRegExp=function(string){
	        var pattern=/^([0-9]|[0-1][0-9]|2[0-3]):(2[0-9]|[0-5][0-9])$/;
	        var flag = pattern.test(string);
	        return flag;
        };
		//验证
		$scope.viewModel.requiredForm= function() { 
			var startFrom ='2000-01-01 '+$scope.editForm.startFromTime.$modelValue;
			var startFromTime = new Date(startFrom.replace(/-/g,"/") );
			var startTo ='2000-01-01 '+$scope.editForm.startToTime.$modelValue;
			var startToTime = new Date(startTo.replace(/-/g,"/") );
			
			var endFrom ='2000-01-01 '+$scope.editForm.endFromTime.$modelValue;
			var endFromTime = new Date(endFrom.replace(/-/g,"/") );
			var endTo ='2000-01-01 '+$scope.editForm.endToTime.$modelValue;
			var endToTime = new Date(endTo.replace(/-/g,"/") );

			if(!$scope.viewModel.escapeRegExp($scope.editModel.punchTime.startFromTime)){
				$scope.editForm.startFromTime.$dirty=true;
				return true;
			}
			if(!$scope.viewModel.escapeRegExp($scope.editModel.punchTime.startToTime)){
				$scope.editForm.startToTime.$dirty=true;
				return true;
			}
			if(!$scope.viewModel.escapeRegExp($scope.editModel.punchTime.endFromTime)){
				$scope.editForm.endFromTime.$dirty=true;
				return true;
			}
			if(!$scope.viewModel.escapeRegExp($scope.editModel.punchTime.endToTime)){
				$scope.editForm.endToTime.$dirty=true;
				return true;
			}
			if(!$scope.viewModel.escapeRegExp($scope.editModel.punchTime.restFromTime)){
				$scope.editForm.restFromTime.$dirty=true;
				return true;
			}
			if(!$scope.viewModel.escapeRegExp($scope.editModel.punchTime.restToTime)){
				$scope.editForm.restToTime.$dirty=true;
				return true;
			}
			if ((!$scope.editForm.punchTimeName.$valid
					|| !$scope.editForm.startFromTime.$valid
					|| !$scope.editForm.startToTime.$valid
					|| !$scope.editForm.startInterval.$valid
					|| !$scope.editForm.endFromTime.$valid
					|| !$scope.editForm.endToTime.$valid
					|| !$scope.editForm.endInterval.$valid) && $scope.editModel.punchType == 0) {
				if(!$scope.editForm.punchTimeName.$dirty){
	        		$scope.editForm.punchTimeName.$dirty=true;
	        	}	
				if(!$scope.editForm.startFromTime.$dirty){
	        		$scope.editForm.startFromTime.$dirty=true;
	        	}
	        	if(!$scope.editForm.startToTime.$dirty){
	        		$scope.editForm.startToTime.$dirty=true;
	        	}
	        	if(!$scope.editForm.endFromTime.$dirty){
	        		$scope.editForm.endFromTime.$dirty=true;
	        	}
	        	if(!$scope.editForm.endToTime.$dirty){
	        		$scope.editForm.endToTime.$dirty=true;
	        	}
	        	if(!$scope.editForm.startInterval.$dirty){
	        		$scope.editForm.startInterval.$dirty=true;
	        	}
	        	if($scope.editModel.punchTime.endInterval == "" || $scope.editModel.punchTime.endInterval == undefined){
	        		$scope.editForm.endInterval.$dirty=true;
	        	}
	        	return true;
       	 	}else if ((!$scope.editForm.punchTimeName.$valid
       	 			|| !$scope.editForm.startFromTime.$valid
					|| !$scope.editForm.startInterval.$valid
					|| !$scope.editForm.endFromTime.$valid
					|| !$scope.editForm.endInterval.$valid) && $scope.editModel.punchType == 1) {
       	 	   if(!$scope.editForm.punchTimeName.$dirty){
        		   $scope.editForm.punchTimeName.$dirty=true;
        	    }
				if(!$scope.editForm.startFromTime.$dirty){
	        		$scope.editForm.startFromTime.$dirty=true;
	        	}
	        	if(!$scope.editForm.endFromTime.$dirty){
	        		$scope.editForm.endFromTime.$dirty=true;
	        	}
	        	if(!$scope.editForm.startInterval.$dirty){
	        		$scope.editForm.startInterval.$dirty=true;
	        	}
	        	if($scope.editModel.punchTime.endInterval == "" || $scope.editModel.punchTime.endInterval == undefined){
	        		$scope.editForm.endInterval.$dirty=true;
	        	}
	        	return true;
       	 	}else{
       	 		if (!$scope.editForm.restFromTime.$valid
					|| !$scope.editForm.restToTime.$valid){
	       	 		if(!$scope.editForm.restFromTime.$dirty){
		        		$scope.editForm.restFromTime.$dirty=true;
		        	}
		       	 	if(!$scope.editForm.restToTime.$dirty){
		        		$scope.editForm.restToTime.$dirty=true;
		        	}
		       	 	return true;
       	 		}

	       		if($scope.editModel.punchTime.startFromTime > $scope.editModel.punchTime.startToTime){
	        		$scope.editForm.startToTime.$invalid=true;
	        		$scope.editForm.startToTime.$dirty=true;
	        		return true;
	        	}else{
	        		$scope.editForm.startToTime.$invalid=false;
	        		$scope.editForm.startToTime.$dirty=false;
	        	}
	        	if($scope.editModel.punchTime.endFromTime > $scope.editModel.punchTime.endToTime){
	        		$scope.editForm.endToTime.$invalid=true;
	        		$scope.editForm.endToTime.$dirty=true;
	        		return true;
	        	}else{
	        		$scope.editForm.endToTime.$invalid=false;
	        		$scope.editForm.endToTime.$dirty=false;
	        	}
       	 		
	       	 	if($scope.editModel.punchTime.restFromTime > $scope.editModel.punchTime.restToTime){
	        		$scope.editForm.restToTime.$invalid=true;
	        		$scope.editForm.restToTime.$dirty=true;
	        		return true;
	        	}else{
	        		$scope.editForm.restToTime.$invalid=false;
	        		$scope.editForm.restToTime.$dirty=false;
	        	}
       	 		
	       		if($scope.editModel.punchTime.startFromTime <= $scope.editModel.punchTime.startToTime && $scope.editModel.punchTime.endFromTime <= $scope.editModel.punchTime.endToTime){
	        		return false;
	        	}else{
	        		return true;
	        	}
       	 	}
        };
		
		//保存
		$scope.viewModel.save = function(){
			if($scope.editModel.punchType==1){
				$scope.editModel.punchTime.startToTime=$scope.editModel.punchTime.startFromTime;
				$scope.editModel.punchTime.endToTime=$scope.editModel.punchTime.endFromTime;
			}
			if($scope.viewModel.requiredForm()){
				return;
			}
			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
				Service.savePunchTime($scope.editModel.punchTime,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("新增成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}else{
				Service.updatePunchTime($scope.editModel.punchTime,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("修改成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}
		};
		
		//删除
		$scope.viewModel.del=function(punchTime){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deletePunchTime({
					punchTimeId:punchTime.punchTimeId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
		
		$scope.viewModel.checkGlobal=function(){
			return false;
		};

} ]);